unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Grids, DBGrids, ComCtrls, Menus, ExtCtrls, Buttons,
  PDO, PDOStatement, PDOClasses,
  DB, BufDataset,
  _data, _db, _nodes, _config, _tabs, _types;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Data_Page_ShowAll: TBitBtn;
    Data_Page_Prev: TBitBtn;
    Data_Page_Next: TBitBtn;
    Button3: TButton;
    MainMenu: TMainMenu;
    Memo1: TMemo;
    Menu_File_Connect: TMenuItem;
    Data_Paginator: TPanel;
    Splitter2: TSplitter;
    StringGrid1: TStringGrid;
    Tabs0_Menu_CloseTab: TMenuItem;
    Menu_Query: TMenuItem;
    Menu_Query_Run: TMenuItem;
    Menu_File: TMenuItem;
    Menu_Edit: TMenuItem;
    Menu_Import: TMenuItem;
    Menu_Help: TMenuItem;
    Menu_File_Open: TMenuItem;
    Menu_File_Exit: TMenuItem;
    Menu_Help_About: TMenuItem;
    Menu_Help_GetHelp: TMenuItem;
    MenuImportFile: TMenuItem;
    Tabs0_Menu_NewQuery: TMenuItem;
    Tabs0_Menu: TPopupMenu;
    Tabs0: TPageControl;
    StatusBar1: TStatusBar;
    Tabs0_Server: TTabSheet;
    Tabs0_Table: TTabSheet;
    Tabs0_Data: TTabSheet;
    Tabs0_Database: TTabSheet;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TreeView1: TTreeView;
    procedure Button1Click(Sender: TObject);
    procedure Data_Page_NextClick(Sender: TObject);
    procedure Data_Page_PrevClick(Sender: TObject);
    procedure Data_Page_ShowAllClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure Menu_File_ExitClick(Sender: TObject);
    procedure Menu_Query_RunClick(Sender: TObject);
    procedure Tabs0_Menu_CloseTabClick(Sender: TObject);
    procedure Tabs0_Menu_NewQueryClick(Sender: TObject);
    procedure TreeView1Click(Sender: TObject);
  private
    { private declarations }

  public
    { public declarations }
  end;

var
  Form1: TForm1;
  t:TColor;
implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Data_Page_NextClick(Sender: TObject);
begin
  _tabs.data.page := _tabs.data.page + 1;
  _tabs.data.loadGrid;
end;

procedure TForm1.Data_Page_PrevClick(Sender: TObject);
begin
  if (_tabs.data.page > 0) then
  begin
    _tabs.data.page := _tabs.data.page - 1;
    _tabs.data.loadGrid;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  //
end;

procedure TForm1.Data_Page_ShowAllClick(Sender: TObject);
begin
  _tabs.data.rows_per_page:=0;
  _tabs.data.loadGrid;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  _tabs.queries.parent:=Tabs0;
  _tabs.queries.Add;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
begin
  if (Key = $78 {F9}) then
    Menu_Query_RunClick(Sender);
end;

procedure TForm1.FormShow(Sender: TObject);
var dsn, user, pass:string;
begin
  _data.log := TAppLog.Create(Memo1);
  _data.log.setLoggingLevel(llDebug);
  _data.log.info('logging started');


  _tabs.data.grid := TAppDataGrid.Create(Tabs0_Data);
  _tabs.data.grid.Parent := Tabs0_Data;
  _tabs.data.grid.Name := 'Tabs0_Data_Grid';
  _tabs.data.grid.Align := alClient;

  _tabs.database := TAppDatabaseTab.Create(Tabs0);
  _tabs.database.grid := TAppDataGrid.Create(Tabs0_Database);
  _tabs.database.grid.Parent := Tabs0_Database;
  _tabs.database.grid.Name := 'Tabs0_Database_Grid';
  _tabs.database.grid.Align := alClient;

  //
  dsn := paramstr(1);
  user := 'root';
  pass := '';
  _data.log.info('Connecting '+user+'@'+dsn);
  if (_db.db.connect(dsn,user,pass)) then
  begin
    _db.db.query_as_is('SET character_set_results=UTF8');
    _db.db.query_as_is('SET character_set_connection=UTF8');
    _db.db.query_as_is('SET character_set_client=UTF8');

    TreeView1.Items.Clear;
  //  TreeView1.BeginUpdate;
    _db.db.treeLoadItems(TreeView1);
  //  TreeView1.EndUpdate;
  end;

end;

procedure TForm1.Menu_File_ExitClick(Sender: TObject);
begin
  Close;
end;

procedure TForm1.Menu_Query_RunClick(Sender: TObject);
var
  i: integer;
  n, v: string;
  memo, grid: TObject;
  stmt: IPDOStatement;
  rowKeyPair: TPDORowKeyStrings;
begin

 exit; ///////////////////////////////////////

  try
    rowKeyPair := TPDORowKeyStrings.Create;
    memo := FindComponent(tabs0.ActivePage.Name + '_Text');
    grid := FindComponent(tabs0.ActivePage.Name + '_Grid');
    with memo as TMemo do
    begin
      Memo1.Lines.Add('Running query: ' + Text);
      stmt := _db.db.query_as_is(Text);
      with grid as TStringGrid do
      begin
        ColCount := stmt.columnCount;
        FixedCols := 0;
        RowCount := 1;
        while (stmt.fetch(rowKeyPair)) do
        begin
          RowCount := RowCount + 1;
          for i := 0 to stmt.columnCount - 1 do
          begin
            rowKeyPair.GetNameValue(i, n, v);
            if (RowCount = 2) then
            begin
              Cells[i, 0] := n;
            end;
            Cells[i, RowCount - 1] := v;
          end;
        end;
      end;
    end;
  except
  end;
end;

procedure TForm1.Tabs0_Menu_CloseTabClick(Sender: TObject);
var
  o: TObject;
begin
  with tabs0.ActivePage do
  begin
    if (copy(Name, 1, 11) = 'Tabs0_Query') then
      if (copy(Name, 12, 1) = '0') then
      begin
        o := Form1.FindComponent('Tabs0_Query0_Text');
        if (o <> nil) then
          with o as TMemo do
            Lines.Clear;
      end
      else
      begin
        PageControl := nil;
        Free();
      end;
  end;
end;

procedure TForm1.Tabs0_Menu_NewQueryClick(Sender: TObject);
begin
  Tabs0.ActivePage:=_tabs.queries.Add;
end;

procedure TForm1.TreeView1Click(Sender: TObject);
var nt:string;
begin
  if (TreeView1.Selected <> nil) then
  begin
    nt:='';
    if (TreeView1.Selected.Data <> nil) then
    begin
      if (PNodeData(TreeView1.Selected.Data)^.nodeType = ntDatabase) then
      begin
        if (not TreeView1.Selected.hasChildren) then
        begin
          _db.db.treeLoadObjects(TreeView1.Selected,TreeView1.Selected.Text);
          TreeView1.Selected.Expanded := true;
        end;
        _tabs.database.loadGrid(TreeView1.Selected.Text);
      end;

      if (PNodeData(TreeView1.Selected.Data)^.nodeType = ntTable) or
         (PNodeData(TreeView1.Selected.Data)^.nodeType = ntView) then
      begin
        Tabs0.ActivePage:=Tabs0_Data;
        _tabs.data.table := TreeView1.Selected.Text;
        _tabs.data.database := TreeView1.Selected.Parent.Parent.Text;
        _tabs.data.rows_per_page:=_tabs.data.rows_per_page_default;
        _tabs.data.loadGrid();
      end;
    end;
  end;
end;

end.
